//
//  CommitAddPartnerViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/22.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "CommitAddPartnerViewController.h"
#import "Partner.h"
#import "BLHeader.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SearchNet.h"
#import "QRCodeView.h"
#import <MBProgressHUD.h>

@interface CommitAddPartnerViewController () <QRCodeViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *blurImage;
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *userTel;
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@property (weak, nonatomic) IBOutlet UILabel *storeLabel;
@property (weak, nonatomic) IBOutlet UILabel *sellLabel;
@property (nonatomic, assign) CGFloat rateValue;
@property (nonatomic, strong) QRCodeView *codeView;


@end

@implementation CommitAddPartnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}


- (void)setupUI {
    self.title = @"确认添加";
    self.rateValue = 10;
    self.commitBtn.layer.cornerRadius = 5;
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:self.partner.headImg] placeholderImage:[UIImage imageNamed:@"icon"]];
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = self.blurImage.frame;
    [self.blurImage sd_setImageWithURL:[NSURL URLWithString:self.partner.headImg] placeholderImage:[UIImage imageNamed:@"icon"]];
    [self.blurImage addSubview:effectView];
    self.userName.text = [NSString stringWithFormat:@"用户名：%@",self.partner.name];
    self.userTel.text = [NSString stringWithFormat:@"电话：%@",self.partner.tel];
    self.rateSlider.minimumValue = 0;
    self.rateSlider.maximumValue = 100;
    [self.rateSlider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
    self.rateSlider.value = 100;
    self.storeLabel.text = @"店主 10.0";
    self.sellLabel.text = @"销售 0.0";
    
}

- (void)sliderValueChange:(UISlider *)slider {
    float yu = (int)slider.value % 10;
    if ( yu < 2 ){
        yu = 0;
    }else if (yu > 2 && yu < 8) {
        yu = 0.5;
    }else {
        yu = 1;
    }
    self.rateValue = (int)slider.value / 10 + yu;
    self.storeLabel.text = [NSString stringWithFormat:@"店主 %0.1f",self.rateValue];
    self.sellLabel.text = [NSString stringWithFormat:@"销售 %0.1f",10 - self.rateValue];
}


- (IBAction)commitAddEvent:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    __weak typeof (self) weakSelf = self;
    [SearchNet commitAddPartner:self.partner.tel rate:self.rateValue completer:^(id result, NSError *error) {
        if (result) {
            [hud hideAnimated:YES];
            weakSelf.codeView.codeURL = result;
            [weakSelf showQRCodeView];
            return;
        }else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = [error localizedDescription];
            [hud hideAnimated:YES afterDelay:2];
        }
    }];
}

- (QRCodeView *)codeView {
    if (_codeView == nil) {
        _codeView = [QRCodeView qrCodeView];
        _codeView.delegate = self;
    }
    _codeView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
    return _codeView;
}


- (void)showQRCodeView {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:_codeView];
    [UIView animateWithDuration:0.2 animations:^{
        _codeView.frame = self.view.frame;
    }];

}


- (void)closeView {
    [_codeView removeFromSuperview];
    [UIView animateWithDuration:0.2 animations:^{
        _codeView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, KScreenHeight);
    }];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定对方扫描成功？" preferredStyle:UIAlertControllerStyleAlert];
   
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self showQRCodeView];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

@end
